﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.DAL;
using Telerik.Web.UI;


namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class TR_TrainingCourses_AddScore : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        HR_Development iniData = new HR_Development();
        ComboBLL combo = new ComboBLL();
        ITrainingScholarshipTypeBLL SchTypeBLL = new TrainingScholarshipTypeBLL();
        InitialData_BLL iniDataCur = new InitialData_BLL();
        ITrainingScholarshipFundBLL SchFundBLL = new TrainingScholarshipFundBLL();
        TrainingInstituteBLL InstituteBLL = new TrainingInstituteBLL();
        ConvertNullable cn = new ConvertNullable();
        TrainingCoursesInfo courseInfo = new TrainingCoursesInfo();
        TrainingCoursesDesDtlsBLL CDesBLL = new TrainingCoursesDesDtlsBLL();


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                LoadDataGrid();
                bindCommandType();
                //LoadTree();
            }

        }

        private void LoadDataGrid()
        {

            DataSet ListDataCommand = CDesBLL.GetData("", "", "1");
            DataTable table = ListDataCommand.Tables[0];
            RadGrid2.DataSource = ListDataCommand.Tables[0];
            RadGrid2.DataBind();

        }

        protected void LoadTree()
        {
            DataSet ListDataCommand = CDesBLL.GetDataTreeView();
            DataTable table = ListDataCommand.Tables[0];
            RadTreeView1.DataSource = ListDataCommand.Tables[0];
            RadTreeView1.DataBind();
        }
        protected void RadTreeView1_NodeClick(object sender, Telerik.Web.UI.RadTreeNodeEventArgs e)
        {

        }

        protected void RadGrid2_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "DataEdit")
            {
                txtSequence.Text = RadGrid2.Items[e.Item.ItemIndex]["trainee_seq"].Text;
                txtEmpSeq.Text = RadGrid2.Items[e.Item.ItemIndex]["emp_seq"].Text;
                txtTrainSeq.Text = RadGrid2.Items[e.Item.ItemIndex]["train_seq"].Text;
                lbTrainee.Text = RadGrid2.Items[e.Item.ItemIndex]["FULL_NAME"].Text;
                string strHour = RadGrid2.Items[e.Item.ItemIndex]["train_hour"].Text;
                if (strHour == "" || strHour == "&nbsp;")
                { txtTrainHour.Text = ""; }
                else
                {   txtTrainHour.Text = RadGrid2.Items[e.Item.ItemIndex]["train_hour"].Text;}

                string strResult = RadGrid2.Items[e.Item.ItemIndex]["T_SCORE"].Text;
                if (strResult == "ผ่าน")
                { rblTrainResult.SelectedValue = "1"; }
                else
                { rblTrainResult.SelectedValue = "0"; }

                string strComment = RadGrid2.Items[e.Item.ItemIndex]["result_memo"].Text;
                if (strComment == "" || strComment == "&nbsp;")
                { txtComment.Text = ""; }
                else
                { txtComment.Text = RadGrid2.Items[e.Item.ItemIndex]["result_memo"].Text; }

                UpdateUI2("Update");   
            }
            else if (e.CommandName == "AddData")
            {
                UpdateUI2("AddNew"); 
            }
            //if (e.CommandName == "ExpandCollapse")
            //    return;
            LoadDataGrid();
        }

        protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
        {
            //    if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            //    {
            //        string scr;
            //        if (e.Item.Cells[6].Text == "1")
            //            scr = "<img src='../Images/Buttons/Update.gif'>";
            //        else
            //            scr = "<img src='../Images/Buttons/Cancel.gif'>";
            //        e.Item.Cells[6].Text = scr;

            //    }
        }
        private void bindCommandType()
        {
            //DataTable InfoList = combo.ListTrainingCourse();

            //ddlType.DataSource = InfoList;
            //ddlType.DataBind();
        }

        protected void ddlType_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }
        protected void btnUpdate2_Click(object sender, EventArgs e)
        {
            //string strJobSite = cbJobSite.SelectedValue;//CDesBLL.GetData(cbEmployeeList.SelectedValue);
            ////string strJobSite = ListJobSite.Tables[0];
            //string TrainResult = rblTrainResult.SelectedValue;
            string User = ssMng.EmpSeq;
            Nullable<int> iTrainResult = cn.ConvertNullInt(rblTrainResult.SelectedValue);
            //string JobSiteSeq = "";
            //string WorkingName = "";
            //string ClassName = "";
            //string PostName = "";
            //DataSet ListSalaryCommand = EmpBLL.LoadEmployees(cn.ConvertNullInt(cbEmployeeList.SelectedValue), "1", true);
            //if (ListSalaryCommand.Tables.Count > 0)
            //{
            //    DataTable table = ListSalaryCommand.Tables[0];
            //    JobSiteSeq = table.Rows[0]["JOB_SITE_SEQ"].ToString();
            //    WorkingName = table.Rows[0]["WORKING_JOB_SITE_NAME"].ToString();
            //    ClassName = table.Rows[0]["CLASS_NAME"].ToString();
            //    PostName = table.Rows[0]["POS_NAME"].ToString();
            //}

            CDesBLL.InsertQuta(txtSequence.Text, txtTrainSeq.Text,txtEmpSeq.Text, txtComment.Text
                                , rblTrainResult.SelectedValue, txtTrainHour.Text, User, "1");

            LoadDataGrid();            
            UpdateUI2("Save");
        }
        protected void btnCancel2_Click(object sender, EventArgs e)
        {
            UpdateUI2("Cancel");
            ClearControl();
        }
        protected void ClearControl()
        {
        }
        protected bool checkValidate()
        {
            bool bOK = true;
            //string strJobSite = cbJobSite.SelectedValue;
            //string strQuota = txtquota.Text;

            //if (strJobSite.Length == 0)
            //{
            //    bOK = false;
            //    lblError2.Text = "* กรุณาเลือกหน่วยงาน";
            //}
            return bOK;
        }
        protected void UpdateUI2(string strCommand)
        {
            switch (strCommand)
            {
                case "Save":
                    PanelGrid2.Visible = true;
                    PanelAddData2.Visible = false;

                    break;

                case "AddNew":
                    PanelGrid2.Visible = false;
                    PanelAddData2.Visible = true;
                                        
                    break;

                case "Cancel":
                    PanelGrid2.Visible = true;
                    PanelAddData2.Visible = false;

                    break;

                case "Update":
                    PanelGrid2.Visible = false;
                    PanelAddData2.Visible = true;
                    
                    break;
            }
        }

    }
}
